package b.b.d.a;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.util.Log;
import android.util.SparseArray;
import android.util.TypedValue;
import b.b.h.v;
import b.i.b.f;
import java.util.WeakHashMap;

@SuppressLint({"RestrictedAPI"})
/* compiled from: AppCompatResources */
public final class a {
    public static final ThreadLocal<TypedValue> a = new ThreadLocal();
    public static final WeakHashMap<Context, SparseArray<a>> b = new WeakHashMap(0);
    public static final Object c = new Object();

    /* compiled from: AppCompatResources */
    public static class a {
        public final ColorStateList a;
        public final Configuration b;

        public a(ColorStateList colorStateList, Configuration configuration) {
            this.a = colorStateList;
            this.b = configuration;
        }
    }

    public static ColorStateList a(Context context, int i) {
        int i2 = VERSION.SDK_INT;
        if (i2 >= 23) {
            return context.getColorStateList(i);
        }
        ColorStateList colorStateList;
        ColorStateList colorStateList2;
        synchronized (c) {
            SparseArray sparseArray = (SparseArray) b.get(context);
            colorStateList = null;
            if (sparseArray != null && sparseArray.size() > 0) {
                a aVar = (a) sparseArray.get(i);
                if (aVar != null) {
                    if (aVar.b.equals(context.getResources().getConfiguration())) {
                        colorStateList2 = aVar.a;
                    } else {
                        sparseArray.remove(i);
                    }
                }
            }
            colorStateList2 = null;
        }
        if (colorStateList2 != null) {
            return colorStateList2;
        }
        Resources resources = context.getResources();
        ThreadLocal threadLocal = a;
        TypedValue typedValue = (TypedValue) threadLocal.get();
        if (typedValue == null) {
            typedValue = new TypedValue();
            threadLocal.set(typedValue);
        }
        boolean z = true;
        resources.getValue(i, typedValue, true);
        int i3 = typedValue.type;
        if (i3 < 28 || i3 > 31) {
            z = false;
        }
        if (!z) {
            resources = context.getResources();
            try {
                colorStateList = f.e0(resources, resources.getXml(i), context.getTheme());
            } catch (Exception e) {
                Log.e("AppCompatResources", "Failed to inflate ColorStateList, leaving it to the framework", e);
            }
        }
        if (colorStateList != null) {
            synchronized (c) {
                WeakHashMap weakHashMap = b;
                SparseArray sparseArray2 = (SparseArray) weakHashMap.get(context);
                if (sparseArray2 == null) {
                    sparseArray2 = new SparseArray();
                    weakHashMap.put(context, sparseArray2);
                }
                sparseArray2.append(i, new a(colorStateList, context.getResources().getConfiguration()));
            }
            return colorStateList;
        }
        ColorStateList colorStateList3;
        Object obj = b.i.c.a.a;
        if (i2 >= 23) {
            colorStateList3 = context.getColorStateList(i);
        } else {
            colorStateList3 = context.getResources().getColorStateList(i);
        }
        return colorStateList3;
    }

    public static Drawable b(Context context, int i) {
        return v.d().f(context, i);
    }
}
